﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using socialnetworkApp.Domain.Dto.Input;
using socialnetworkApp.Domain.Dto.Output;
using socialnetworkApp.Domain;
using socialnetworkApp.Service.AdminService;
using Microsoft.AspNetCore.Authorization;
using socialnetworkApp.Domain.Manager;
using System.Text.Json;

namespace socialnetworkApp.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AdminController : ControllerBase
    {
        public IAdminService _adminService;
        public AdminController(IAdminService adminService) { 
        _adminService = adminService;
        }



        /// <summary>
        /// 用户管理接口
        /// </summary>
        /// <param name="pageNo"></param>
        /// <param name="pageSize"></param>
        /// <param name="username">查询的用户</param>
        /// <returns></returns>
        [HttpGet("getUserList/{pageNo}/{pageSize}")]
        [Authorize]
        public async Task<ResponseResult<UsersListOutput>> GetUserListInfo(int pageNo, int pageSize, [FromQuery] string? username){

            var uid = User.GetUserId();
            //var uid = "1";
            GetUserListInput Id = new GetUserListInput() { Id = uid, PageNo= pageNo, PageSize= pageSize,UserName=username };
            return await _adminService.GetUserListInfo(Id);
        }
       /// <summary>
       /// 获取当前用户所拥有的职位
       /// </summary>
       /// <param name="userid">当前用户Id</param>
       /// <returns></returns>
        [HttpGet("getUserRolesById/{userid}")]
        public async Task<ResponseResult<toAssignRoleListOutput>> toAssign(string userid)
        {

            return await _adminService.toAssign(userid);
        }

        /// <summary>
        ///删除当前用户
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <returns></returns>
        [HttpPost("removeUser")]
        public async Task<ResponseResult<string>> RemoveUser([FromQuery]string userid) {
         return await _adminService.DeleteUser(userid);

        }
        [HttpPut("updateUser")]
        public async Task<ResponseResult<string>> EditUser([FromBody]InputUserDto inputUser)
        {
            return await _adminService.EditUser(inputUser);

        }

        /// <summary>
        /// 给用户分配权限
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("doAssignRole")]
        public async Task<ResponseResult<string>> DoAssignRole([FromBody] AssignRoleInput request)
        {
          
            return await _adminService.UserAssignRoleAsync(request);
        }

       /// <summary>
       /// 创建新用户
       /// </summary>
       /// <param name="user"></param>
       /// <returns></returns>
        [HttpPost("userinfoSave")]
    
            public async Task<ResponseResult<string>> CreateUser([FromBody] AddUserInput user)
            {
            return await _adminService.CreateUser(user);
            }
        



        /// <summary>
        /// 获取角色（职务列表）
        /// </summary>
        /// <param name="pageNo"></param>
        /// <param name="pageSize"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        [HttpGet("getRoleList/{pageNo}/{pageSize}")]
        public async Task<ResponseResult<RoleListOutput>> GetRoleListInfo(int pageNo, int pageSize, [FromQuery] string? roleName) {

            var uid = User.GetUserId();
            GetRoleListInput Id = new GetRoleListInput() { Id = uid, PageNo = pageNo, PageSize = pageSize, RoleName = roleName };
            return await _adminService.GetRoleListInfo(Id);
        }
       /// <summary>
       /// 添加新角色（职位）
       /// </summary>
       /// <param name="input"></param>
       /// <returns></returns>
        [HttpPost("addRoleInfo")]
        [Authorize]
        public async Task<ResponseResult<string>> AddRoleInfo([FromBody] AddRoleInfoInput input)
        {

           return await _adminService.AddRoleInfo(input); // 假设服务方法已重命名为AddRoleInfo以匹配控制器方法  
         
        }
        /// <summary>
        /// 获取菜单权限
        /// </summary>
        /// <returns></returns>

        [HttpGet("getPermissionInfo")]
        public async Task<ResponseResult<List<GetPermissionInfoOutput>>> GetPermissionInfo() { 
        
        
            return await _adminService.GetPermissionInfo();
        }
        /// <summary>
        /// 添加新菜单权限和页面功能权限
        /// </summary>
        /// <param name="addPermission"></param>
        /// <returns></returns>
        [HttpPost("PermissionSave")]

        public async Task<ResponseResult<string>> CreatePermission([FromBody] AddPermissionDto addPermission)
        {
            return await _adminService.AddPermissionInfo(addPermission);
        }
        /// <summary>
        /// 根据菜单id删除菜单包括子类
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        [HttpDelete("removePermissionInfo/{pId}")]
        public async Task<ResponseResult<string>> RemovePermissionInfo(string pId) {
            return await _adminService.RemovePermissionInfo(pId);
                }


        /// <summary>
        /// 根据角色id，获取菜单权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet("getPermissionInfoById/{roleId}")]
        public async Task<ResponseResult<List<GetPermissionInfoOutput>>> GetPermissionInfoById( string roleId)
        {

         return await    _adminService.GetPermissionInfoById(roleId);
        }


       /// <summary>
       /// 给（角色）职位分配权限
       /// </summary>
       /// <param name="roleId">职位id</param>
       /// <param name="permissionId"></param>
       /// <returns></returns>

        [HttpPost("doAssign")]
        public async Task<ResponseResult<string>> AssignPermissionsToRole([FromQuery] string roleId, [FromQuery] List<string> permissionId)
        {
            //使用 Split() 方法将字符串按照逗号分割成字符串数组，然后将数组中的元素逐个添加到新的 List<string> 中  因为参数前端的数组穿过来只传到了list的第一个item上面
            List<string> permissionIdList = permissionId[0].Split(',').ToList();
            return await   _adminService.AssignPermissionsToRole(roleId, permissionIdList);
          

        }
        }
    }
